package com.easygem.front.service.imp;

import com.easygem.business.service.base.AbstractServiceImpl;
import com.easygem.common.error.SystemCodeEnum;
import com.easygem.common.error.SystemException;
import com.easygem.common.model.system.User;
import com.easygem.common.response.ActiveUser;
import com.easygem.common.vo.front.SeatVO;
import com.easygem.common.vo.system.DepartmentVO;
import com.easygem.common.vo.system.PageVO;
import com.easygem.front.converter.SeatConverter;
import com.easygem.front.mapper.DepartmentMapper;
import com.easygem.front.mapper.SeatMapper;
import com.easygem.front.service.DepartmentService;
import com.easygem.front.service.SeatService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 廖思炜
 * @version 1.0
 * @className SeatServiceImpl
 * @Description TODO 坐席服务层实现类
 * @date 2025/3/25 9:14
 */
@Service
@Transactional
public class SeatServiceImpl extends AbstractServiceImpl<SeatMapper,User,Integer> implements SeatService {
    @Autowired
    private SeatMapper seatMapper;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
     private DepartmentMapper departmentMapper;

    /**
     * 获得坐席列表
     * @param pageNum
     * @param pageSize
     * @param seatVO
     * @return
     * @throws SystemException
     */
    @Transactional(readOnly = true)
    @Override
    public PageVO<User> findList(Integer pageNum, Integer pageSize, SeatVO seatVO) throws SystemException {

        User user = new User();

        BeanUtils.copyProperties(seatVO, user);

        Map<String,Object> map = new HashMap<>();

        map.put("user",user);
        PageHelper.startPage(pageNum, pageSize);
        List<User> seatList=seatMapper.selectSeatsList(map);

        PageInfo<User> info=new PageInfo<>(seatList);
        return new PageVO<>(info.getTotal(),seatList);
    }

    /**
     * 获得部门列表
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public List<DepartmentVO> getAllDeptList() {
        return departmentService.getDepartmentListByUser();

    }

    /**
     * 更新坐席状态
     * @param seatVO
     * @return
     * @throws SystemException
     */
    @Override
    public Integer setSeatStatus(SeatVO seatVO) throws SystemException {
        User user = seatMapper.selectByPrimaryKey(seatVO.getUserId());

        if (user == null) {
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR);
        }else {
            if (user.getStatus()==-1||user.getStatus()==-2){
                throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"该用户无法更改状态");
            }
        }
        Map<String,Object> map=selectSeatsCount(user.getAccountName());

        if (map.get("maxSeatsNumbers")!=null){
            //判断是否为开启坐席操作
            if(seatVO.getStatus()==1){
                Integer maxSeatsNumbers=Integer.parseInt(String.valueOf(map.get("maxSeatsNumbers")));
                Integer useSeatsNumbers=Integer.parseInt(String.valueOf(map.get("useSeatsNumbers")));
                if (useSeatsNumbers>maxSeatsNumbers||useSeatsNumbers.equals(maxSeatsNumbers)){

                    throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"该用户所属账号坐席数量已达最大值");
                }
            }
        }
        BeanUtils.copyProperties(seatVO, user);
        return seatMapper.updateByPrimaryKeySelective(user);
    }

    /**
     * 绑定手机号
     * @param seatVO
     * @return
     * @throws SystemException
     */
    @Override
    public Integer bindPhone(SeatVO seatVO) throws SystemException {
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        if (activeUser.getUser().getStatus()!=1){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"未开启外呼，无法绑定号码");
        }
        if (activeUser.getUser().getIsValidate()==1){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"已绑定号码，请先解除绑定");
        }
        User user=new User();
        user.setUserId(activeUser.getUser().getUserId());
        user.setPhone(seatVO.getPhone());//号码
        user.setIsValidate(1);
        return seatMapper.updateIsValidate(user);
    }

    /**
     * 解绑号码
     *
     * @return
     * @throws SystemException
     */
    @Override
    public Integer unBindingPhone() throws SystemException {
        ActiveUser activeUser=(ActiveUser) SecurityUtils.getSubject().getPrincipal();
        if (activeUser.getUser().getIsValidate()==0){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"未绑定号码！");
        }
        User user=new User();
        user.setUserId(activeUser.getUser().getUserId());
        user.setIsValidate(0);
        return seatMapper.updateByPrimaryKeySelective(user);
    }

    /**
     * 得到当前已验证过的号码，若未经过验证则判定为未绑定
     *
     * @return
     */
    @Override
    public String getBindPhone() {
        ActiveUser activeUser=(ActiveUser) SecurityUtils.getSubject().getPrincipal();
        if (activeUser.getUser().getIsValidate()==1){
            return activeUser.getUser().getPhone();
        }else if (activeUser.getUser().getStatus()==0){
            return null;
        }else {
            return null;
        }
    }

    /**
     * 得到当前已设置的坐席数量与最大坐席数量
     *
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public Map<String, Object> selectSeatsCount(String accountName) {
        Map<String, Object> seatsCount=new HashMap<>();
        if(accountName==null){
            return seatsCount;
        }
        seatsCount.put("maxSeatsNumbers",seatMapper.getMaxSeatsCount(accountName));
        seatsCount.put("useSeatsNumbers",seatMapper.getUsingSeatsCount(accountName));
        System.out.println(seatsCount);
        return seatsCount;
    }

    /**
     * 检查状态是否存在
     *
     * @param user
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public Integer checkStatus(User user) {
        return seatMapper.checkStatus(user);
    }

    /**
     * 得到相同查询条件下的所有坐席
     */
    @Transactional(readOnly = true)
    @Override
    public List<SeatVO> getAllSeatVO(SeatVO seatVO) throws SystemException {
        Map<String,Object> map = new HashMap<>();
        User user = new User();

        BeanUtils.copyProperties(seatVO, user);

        user.setAccountName(seatVO.getAccountName());
        map.put("user",user);

        List<User> getUsers=seatMapper.getAllSeatVO(map);
        return SeatConverter.convert(getUsers);
    }

}
